package net.game.server.util.graph.impl;

import net.game.server.util.graph.Graph;

import java.util.Iterator;

public class ArrayGraph<E> implements Graph<E> {
    private E[] entries;
    private int cap;
    private int offset = 0;
    
    @SuppressWarnings("unchecked")
    public ArrayGraph(int cap) {
        this.cap = cap;
        entries = (E[]) new Object[cap];
    }
    
    @Override
    public void plot(int idx, E e) {
        if(hasSpace()) {
            entries[idx] = e;
            offset++;
        } else {
            throw new ArrayIndexOutOfBoundsException();
        }
    }

    @Override
    public void plot(int idx, E[] e) {
        for(int i = idx; i < e.length; i++) {
            plot(i, e[i]);
        }
    }

    @Override
    public void plotNext(E e) {
        plot(offset+1,e);
    }

    @Override
    public void plotNext(E[] e) {
        for(int i = offset; i < e.length; i++) {
            plot(i, e[i]);
        }
    }

    @Override
    public void remove(int idx) {
        if(idx >= 0 && idx <= entries.length) {
            entries[idx] = null;
            --offset;
        }
    }

    @Override
    public void remove(E e) {
        entries[indexOf(e)] = null;
    }

    @Override
    public boolean contains(E e) {
        return false;
    }

    @Override
    public int size() {
        return entries.length;
    }

    @Override
    public int capacity() {
        return cap;
    }

    @Override
    public Iterator<E> iterator() {
        return null;
    }

    @SuppressWarnings("LoopStatementThatDoesntLoop")
    @Override
    public int indexOf(E e) {
        for(int i = 0; i < entries.length; i++) {
            if(entries[i] == e)
                return i;
            else 
                throw new NullPointerException();
        }
        return -1;
    }
    
    @Override
    public boolean hasSpace() {
        return offset <= cap && offset >= 0;
    }
}
